package com.alibaba.org.service.impl;

import com.alibaba.basic.businessexception.BusinessException;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.basic.util.Md5Utils;
import com.alibaba.basic.util.StrUtils;
import com.alibaba.org.domain.Emp;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.dto.ShopDto;
import com.alibaba.org.mapper.EmpMapper;
import com.alibaba.org.mapper.ShopAuditLogMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.org.service.IShopService;
import com.alibaba.user.domain.Logininfo;
import com.alibaba.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**=====================================================================================
 * 商家入驻逻辑步骤：
 * 一：检验
 * 1.1.每个参数的空值判断
 * 1.2.两次密码不一致校验
 * 1.3.该店铺是否已经被入驻过
 * 二：入驻业务
 * 2.1.保存店铺管理员信息t_employee
 * 2.2.保存店铺信息t_shop
 * 2.2.将店铺的id更新到t_employee中
 ======================================================================================*/
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void settlement(Shop shop) {
        if(StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺名称不能为空!");
        }
        Shop shopTemp = shopMapper.findByNameAndAddress(shop);
        if(shopTemp != null){
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName()+shop.getAddress())){
            throw new BusinessException("店铺名称或者地址非法，请重新输入！");
        }
        // 添加管理员

        Emp admin = shop.getAdmin();
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);
        // 保存logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        admin.setLogininfo_id(logininfo.getId());
        admin.setLogininfo_id(logininfo.getId());
        empMapper.add(admin);

        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        admin.setShop_id(shop.getId());
        empMapper.update(admin);
    }

    @Override
    @Transactional
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.通过了就修改状态，从1审核改成2待激活
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());

        shop.setState(2);

        // 更新数据
        shopMapper.update(shop);

        // 2.添加审核日志
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.add(shopAuditLog);

        // 3.发送邮件确认激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("378509875@qq.com");

        // 查询注册员工的邮箱
        Emp emp = empMapper.findById(shop.getAdminId());
        helper.setTo(emp.getEmail());

        helper.setSubject("激活店铺");

        helper.setText("<h1>店铺激活！！</h1>>",true);
        //添加附件
        helper.setText("<h1>店铺激活邮件!!!</h1><h3><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShopId()
                +"'>点击该链接激活店铺</a></h3>",true);
        //收件人

        javaMailSender.send(mimeMessage);
    }

    @Override
    @Transactional
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        // 1.拒绝了就修改状态，从1审核改成4
        Shop shop = shopMapper.findById(shopAuditLog.getShopId());

        shop.setState(4);

        // 更新数据
        shopMapper.update(shop);

        // 2.添加审核日志
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(350L);
        shopAuditLogMapper.add(shopAuditLog);

        // 3.发送邮件确认激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("378509875@qq.com");

        // 查询注册员工的邮箱,并且填写发送有点地址
        Emp emp = empMapper.findById(shop.getAdminId());
        helper.setTo(emp.getEmail());

        helper.setSubject("入驻失败");

        helper.setText("<h1>入驻失败，请重新填写信息再次入驻，如再次失败，请拨打人工电话！！</h1>>",true);
        //添加附件
        helper.setText("<h3><a href='http://localhost:8081/#/register'>重新入驻链接</a></h3>",true);

        //收件人

        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getEchartsData() {
        List<ShopDto> shopDtos = shopMapper.getEchartsData();
        return shopDtos;
    }

}
